package ee.falcon.controller.profile;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ee.falcon.gui.frame.ProfilesFrame;
import ee.falcon.xml.SpecificProfileInfo;

public class JListSelectionListener implements ListSelectionListener
{
	private final int JLIST_NO_SELECTION_MADE_INDEX = -1; 
	private JList<SpecificProfileInfo> profileNamesList; 
	private ProfilesFrame profilesFrame;
	
	public JListSelectionListener(ProfilesFrame profilesFrame, JList<SpecificProfileInfo> profileNamesList)
	{
		this.profilesFrame = profilesFrame; 
		this.profileNamesList = profileNamesList; 
	}

	@Override
	public void valueChanged(ListSelectionEvent listSelectionEvent) 
	{ 
  	  	if (listSelectionEvent.getValueIsAdjusting() == false) 
	    {
	  	//when there is no selection made in JList
	        if (profileNamesList.getSelectedIndex() == JLIST_NO_SELECTION_MADE_INDEX) 
	        {
		        //No selection, disable fire button.
		         profilesFrame.getEditProfileButton().setEnabled(false); 
		         profilesFrame.getLoginButton().setEnabled(false); 
		         profilesFrame.getDeleteProfileButton().setEnabled(false); 
		         profilesFrame.getNewUserButton().setEnabled(true); 
	    	} 
			else 
			{
				profilesFrame.getEditProfileButton().setEnabled(true); 
				profilesFrame.getLoginButton().setEnabled(true); 
				profilesFrame.getDeleteProfileButton().setEnabled(true);
				profilesFrame.getNewUserButton().setEnabled(false);        	
	        	// Get the first (in our case the only) selected item
	        	profilesFrame.setFirstJListSelection((SpecificProfileInfo) profileNamesList.getSelectedValue());
 	        }
	    }
  	 }
	
	//[START]GETTERS AND SETTERS
	public int get_JLIST_NO_SELECTION_MADE_INDEX()
	{
		return this.JLIST_NO_SELECTION_MADE_INDEX; 
	}
	
	public JList<SpecificProfileInfo> getProfileNamesList()
	{
		return this.profileNamesList; 
	}
	
	public ProfilesFrame getProfilesFrame()
	{
		return this.profilesFrame;
	}
	//[END]GETTERS AND SETTERS
}
